//created by: Lim Yi Le
Ext.define('MajorProjectSenchaTouch.controller.ShelfLocator', {
    extend: 'Ext.app.Controller',
    requires: ['MajorProjectSenchaTouch.store.ShelfLocator'],
    config:{
        views: ['ShelfLocator', 'DetailsViewModule.AvailTab', 'BookMarkedModule.ResultListItem'],
        stores: ['ShelfLocator'],
        refs:{
            shelfLocatorPage: 'shelflocatorpage',
            availTab: 'availTab',
            resultListItem: 'rlistItem',
            shelfLocatorStore: '#shelflocatorstore',
            navView: 'mainNavView'
        },
        control:{
            resultListItem: {
                shelfLocatorBtnTap : 'loadLocationData'
            }
        }
    },
    /*
|--------------------------------------------------------------------------
| loadLocationData (@author : Lim Yi Le)
|--------------------------------------------------------------------------
|
| This function checks the availability of the shelfnumber data for the current selected item
| and loads the collection and shelfnumber data from the availability tab for processing
| in the processLocationData function.
|    
*/
    loadLocationData: function(item, button, e){
        var record = item.getRecord();
        var callnumber = record.data.callnumber;
        if(callnumber != ''){        
            var dvControl = MajorProjectSenchaTouch.app.getController('DetailsView');
            dvControl.getAvail(null, null, item, button);
        }
        e.stopEvent();
    },
    /*
|--------------------------------------------------------------------------
| processLocationData (@author : Lim Yi Le)
|--------------------------------------------------------------------------
|
| This function processes the collection and shelfnumber data from the availability
| tab and matches it with the correct image filename.
|    
*/
    processLocationData: function(collection, shelfnumber){
        var collectionAndLevelArray = '';
        var collectionNew = '';
        var level = '';
        var callnumber = '';
        
        // level and collection portions of image filename specially for AV items
        if(collection == 'Music CD, LRC, Level 4' || collection == 'DVD, LRC, Level 4' || collection == 'VCD, LRC, Level 4' || collection == 'Acc. Materials, CD'){
            level = 'level4';
            collectionNew = 'av.png';
        }
        else{
            // splits the collection data with comma as a delimiter
            collectionAndLevelArray = collection.split(', ');
            // removes spaces and changes the characters to lowercase
            collectionNew = collectionAndLevelArray[0].replace(' ', '').toLowerCase();
            level = collectionAndLevelArray[1].replace(' ', '').toLowerCase();
            
            // retrieves the first two characters of the shelfnumber
            var shelfnumber1 = shelfnumber.charAt(0);
            var shelfnumber2 = shelfnumber.charAt(1);
            
            // checks if the 2nd character of the shelfnumber is an alphabet, if no, remove it
            var regularExp = new RegExp('[A-Z]');
            if(!regularExp.test(shelfnumber2)){
                shelfnumber2 = '';
            }
        }
        
        // loads image filenames from the store which got the data from server-side php reading of image
        // filenames in directory.
        var slStore = Ext.getStore('ShelfLocator');
        slStore.load({
            callback: function(){
                // loop with the number of image filenames found (loop through every image filename)
                for (i = 0; i < slStore.getCount(); i++) {
                    
                    // retrieves the current image filename
                    var currentImageFilename = slStore.getAt(i).data.image;
                    // splits the image filename by the '_' delimiter
                    // (e.g. lib_level6_openshelf_A_G gives lib, level6, openshelf, A, G)
                    var currentImageFilenameArray = currentImageFilename.split('_');
                    
                    // only for image filenames with callnumber
                    if(currentImageFilenameArray.length >= 4){
                        
                        //get the level and collection split values of image filenames
                        // for comparison with level and collection data from availability tab/libcat
                        var imageFilenameLevel = currentImageFilenameArray[1];
                        var imageFilenameCollection = currentImageFilenameArray[2];
                        if(imageFilenameLevel == level & imageFilenameCollection == collectionNew){
                            //only process the callnumber matching if the level and collection is equal
                            var startCallnumber = currentImageFilenameArray[3];
                            var endCallnumber = currentImageFilenameArray[4];
                            var startCallnumber1 = startCallnumber.charAt(0);
                            var startCallnumber2 = startCallnumber.charAt(1);
                            var endCallnumber1;
                            var endCallnumber2;
                            if(endCallnumber == undefined){
                                endCallnumber1 = '';
                                endCallnumber2 = '';
                                if(startCallnumber2 == '.'){
                                    startCallnumber2 = '';
                                }
                            }
                            else if (endCallnumber != undefined){
                                endCallnumber1 = endCallnumber.charAt(0);
                                endCallnumber2 = endCallnumber.charAt(1);
                            
                                if(startCallnumber2 == undefined){
                                    startCallnumber2 = '';
                                }
                                if(endCallnumber2 == '.'){
                                    endCallnumber2 = '';
                                }
                            }

                            var callnumberIntermediate; 
                            if(endCallnumber == undefined){
                                callnumberIntermediate = startCallnumber;
                            }
                            else{
                                callnumberIntermediate = startCallnumber + '_' + endCallnumber;
                            }
                        
                            if(endCallnumber1 == '' & shelfnumber1 == startCallnumber1){
                                if(shelfnumber2 != '' & startCallnumber2 != '' & shelfnumber2 >= startCallnumber2){
                                    callnumber = callnumberIntermediate;
                                }
                                else if(shelfnumber2 != '' & startCallnumber2 == ''){
                                    callnumber = callnumberIntermediate;
                                }
                                else if(shelfnumber2 == ''){
                                    callnumber = callnumberIntermediate;
                                }
                            }
                            else if(shelfnumber1 > startCallnumber1 & shelfnumber1 < endCallnumber1){
                                callnumber = callnumberIntermediate;
                            }
                            else if(shelfnumber1 > startCallnumber1 & shelfnumber1 == endCallnumber1){
                                if(shelfnumber2 != '' & endCallnumber2 != '' & shelfnumber2 <= endCallnumber2){
                                    callnumber = callnumberIntermediate;
                                }
                                else if(shelfnumber2 != '' & endCallnumber2 == '' ){
                                    callnumber = callnumberIntermediate;
                                }
                                else if(shelfnumber2 == ''){
                                    callnumber = callnumberIntermediate;
                                }
                            }
                            else if(shelfnumber1 == startCallnumber1 & shelfnumber1 < endCallnumber1){
                                if(shelfnumber2 != '' & startCallnumber2 != '' & shelfnumber2 >= startCallnumber2){
                                    callnumber = callnumberIntermediate;
                                }
                                else if(shelfnumber2 != '' & startCallnumber2 == '' ){
                                    callnumber = callnumberIntermediate;
                                }
                                else if(shelfnumber2 == ''){
                                    callnumber = callnumberIntermediate;
                                }
                            }
                            else if(shelfnumber1 == startCallnumber1 & shelfnumber1 == endCallnumber1){
                                if(shelfnumber2 != '' & startCallnumber2 != '' & shelfnumber2 >= startCallnumber2 & endCallnumber2 != '' & shelfnumber2 <= endCallnumber2){
                                    callnumber = callnumberIntermediate;
                                }
                                else if(shelfnumber2 != '' & startCallnumber2 != '' & shelfnumber2 >= startCallnumber2 & endCallnumber2 == ''){
                                    callnumber = callnumberIntermediate;
                                }
                                else if(shelfnumber2 != '' & endCallnumber2 != '' & shelfnumber2 <= endCallnumber2 & startCallnumber2 == ''){
                                    callnumber = callnumberIntermediate;
                                }
                                else if(shelfnumber2 != '' & startCallnumber2 == '' & endCallnumber2 == ''){
                                    callnumber = callnumberIntermediate;
                                }
                                else if(shelfnumber2 == ''){
                                    callnumber = callnumberIntermediate;
                                }
                            }
                        }
                    }
                }
            
                var directory = MajorProjectSenchaTouch.app.imagesDirectory;
                var dynamicImg = '';
                if(collection == 'Music CD, LRC, Level 4' || collection == 'DVD, LRC, Level 4' || collection == 'VCD, LRC, Level 4' || collection == 'Acc. Materials, CD'){
                    // image url specially for av items (lib_level4_av.png)
                    dynamicImg = directory + 'lib_' + level + '_' + collectionNew;
                }
                else if (callnumber == ''){
                    // image url for filenames without callnumber (e.g. lib_level5_annualreports.png)
                    dynamicImg = directory + 'lib_' + level + '_' + collectionNew + '.png';
                }
                else {
                    // general image url with callnumber
                    dynamicImg = directory + 'lib_' + level + '_' + collectionNew + '_' + callnumber;
                }
            
                var fpControl = MajorProjectSenchaTouch.app.getController('ShelfLocator');
                fpControl.goOpenShelfLocator(dynamicImg, collection, shelfnumber);
            }
        });        
        
    },
     /*
|--------------------------------------------------------------------------
| goOpenShelfLocator (@author : Lim Yi Le)
|--------------------------------------------------------------------------
|
| This function triggers the opening of the shelf locator view with the collection
| location and shelfnumber data of the current selected item as well as the
| correct corresponding image displayed.
|    
*/
    goOpenShelfLocator: function(imgDirectory, collection, shelfnumber){     
        var ctrl = this;
        var previousPage = ctrl.showMask();
        
        var task = Ext.create('Ext.util.DelayedTask', function(){
            var shelfLocPage = ctrl.getShelfLocatorPage();
            var csPanel = Ext.getCmp('collectionShelfnumberPanel');
            var img = Ext.getCmp('shelfImage');
            
            if(shelfLocPage != undefined){
                shelfLocPage.destroy();
            }
            ctrl.pushView('Shelf Locator', 'shelflocatorpage');
            var titleCollectionShelfnumber = '<div class="shelfTitleWrapper"><div class="shelfBtnLocator"></div><header class="collectionTitle"><h1>' + collection + '</h1></header>' + 
            '<header class="shelfnumber"><h2>' + shelfnumber + '</h2></header></div>';

            csPanel = Ext.getCmp('collectionShelfnumberPanel');
            img = Ext.getCmp('shelfImage');

            // for collection and shelfnumber title
            csPanel.setHtml(titleCollectionShelfnumber);
            
            // for image
            img.setSrc(imgDirectory);
            ctrl.hideMask(previousPage);
        });
        task.delay(1);
    },
        /*
|--------------------------------------------------------------------------
| pushView (@author : Justina Lee)
|--------------------------------------------------------------------------
|
| Set the title of the navigation bar and push the page into the navigation
| view.
|
*/ 
    pushView: function(setTitle, setXtype){
        var view = this.getNavView();
        
        view.push({
            title: setTitle,
            xtype: setXtype
        })
    },
        /*
|--------------------------------------------------------------------------
| showMask (@author : Justina Lee)
|--------------------------------------------------------------------------
|
| Get the current item in the navigation view and set the loading mask.
| Return the current item as previousPage.
|
*/  
    showMask: function(){
        var view = this.getNavView();
        var previousPage = view._activeItem;
        
        previousPage.setMasked({
            xtype: 'loadmask',
            message: 'Please wait...'
        });
        
        return previousPage;
    },
        /*
|--------------------------------------------------------------------------
| hideMask (@author : Justina Lee)
|--------------------------------------------------------------------------
|
| Hide the loading mask of the page.
|
*/  
    hideMask: function(previousPage){        
        previousPage.setMasked(false);
    }
    
});


